package com.huyi.common.config;

import java.nio.charset.StandardCharsets;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

/**
 * 外部接口调用类
 * 
 * @author shengli.hu
 */
@Configuration
public class RestTemplateConfig {

	/**
	 * 初始化RestTemplate,并加入spring的Bean工厂，由spring统一管理
	 * 
	 * @param factory
	 * @return
	 */
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
    	RestTemplate restTemplate = new RestTemplate();
    	// 解决中文乱码问题
    	restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
    	restTemplate.setRequestFactory(factory);
        return restTemplate;
    }
    
    /**
     * 创建HTTP客户端工厂
     * 
     * @return
     */
    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(5000);	//连接超时
        factory.setReadTimeout(5000);		//读取超时
        return factory;
    }

}
